/**
 * 
 */
package com.dava.helpmywife;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.dava.helpmywife.data.initDataApp;

/**
 * @author Dava
 * 
 */
public class inits extends Activity {
	private boolean isAlert;
	private boolean isCall;
	private boolean isSendMsg;
	private boolean isShorIco;
	private String tag="SoftLog";
	public  initDataApp data;
	/**
	 * 初始化参数信息
	 */
	public inits(Activity act) {
		Log.i(tag, "get application data!");
		// 获得设置信息
		data = (initDataApp)act.getApplication();  
		isAlert = data.isAlert();
		isCall = data.isCall();
		isSendMsg = data.isSendMsg();
		isShorIco = data.isShorIco();
		Log.i(tag, "finish app data"+data.isAlert()+data.isCall()+data.isSendMsg());
	}
	/**
	 * 弹出提示框
	 */
	public boolean init(final Activity act,final String phone_number, final String sms_content) {
		/**
		 * 如果设置都为不初始化，则直接结束
		 */
		if (isSendMsg == false && isCall == false && isAlert == false) {
			return false;
		}
			if (isAlert) {
				Dialog alertDialog = new AlertDialog.Builder(act).setTitle("开始求助")
						.setMessage("点击灰色区域 “取消”选择")
						.setPositiveButton("发短信并打电话", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,int which) {
								//打电话
								Call call = new Call();
								call.call(act,phone_number);
								//发短信
								sendMessage sm = new sendMessage();
								if (sm.sendMessage(phone_number, sms_content)) {
									Toast.makeText(act, "短信通知已发送", Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(act, "短信通知发送失败，5S后将再次发送", Toast.LENGTH_SHORT)
											.show();
									sm.sendMessage(phone_number, sms_content);
								}
								data.setCall(true);
								data.setSendMsg(true);
							}
						}).setNegativeButton("打电话", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Call call = new Call();
								call.call(act,phone_number);
								data.setCall(true);
								data.setSendMsg(false);
							}
						}).setNeutralButton("发短信", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								//发短信
								sendMessage sm = new sendMessage();
								if (sm.sendMessage(phone_number, sms_content)) {
									Toast.makeText(act, "短信通知已发送", Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(act, "短信通知发送失败，5S后将再次发送", Toast.LENGTH_SHORT)
											.show();
									sm.sendMessage(phone_number, sms_content);
								}
								data.setCall(false);
								data.setSendMsg(true);
							}
						}).create();
				alertDialog.show();
			}else{
				/**
				 * 打电话。判断是否自动打电话
				 */
				if (isCall) {
					Call call = new Call();
					call.call(act,phone_number);
				}
				/**
				 * 发短信。判断是否自动发送短信
				 */
				if (isSendMsg) {
					sendMessage sm = new sendMessage();
					if (sm.sendMessage(phone_number, sms_content)) {
						Toast.makeText(act, "短信通知已发送", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(act, "短信通知发送失败，5S后将再次发送", Toast.LENGTH_SHORT)
								.show();
						sm.sendMessage(phone_number, sms_content);
					}
				}
			}
			Log.i(tag, "finish app alert!");
			return true;
	}

}
